package com.huiquan.zs.controller;

import com.huiquan.analysis.constants.AnalysisSentenceConstant;
import com.huiquan.analysis.domain.AnalysisRule;
import com.huiquan.analysis.domain.User;
import com.huiquan.analysis.service.AnalysisRuleService;
import com.huiquan.foundation.constant.TabID;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.zs.service.ZsRuleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
public class ZsRuleController {

    @Autowired
    private ZsRuleService zsRuleService;

    /**
     * 主诉进入规则列表画面
     *
     * @param req
     * @param resp
     * @return
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/zs/rule_list.htm")
    public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String tabid = req.getParameter("tabid");
        // 维护页面参数到session
        Map<String, Object> pageParam;
        if (tabid != null && !tabid.isEmpty()) {
            pageParam = new HashMap<>();
            pageParam.put("tabid", tabid);
            req.getSession().setAttribute("pageParam", pageParam);
        } else {
            pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
        }

        // 获取页面参数，如果request中有值则修改session，否则从session中拿值
        String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
        String wordKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "wordKey");

        Map<String, Object> map =  zsRuleService.zsRuleList(start,  wordKey);
        return new ModelAndView("zs/rule_list", map);
    }

    /**
     * 进入规则编辑画面
     *
     * @param req
     * @param resp
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/zs/rule_pre_edit.htm")
    public ModelAndView preEdit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String idStr = req.getParameter("id");
        String vocabulary = req.getParameter("vocabulary");

        Map<String, Object> map =  zsRuleService.zsRulePreEdit(idStr, vocabulary);
        return new ModelAndView("zs/rule_edit", map);
    }

    /**
     * 进行规则编辑操作
     *
     * @param req
     * @param resp
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @ResponseBody
    @RequestMapping(value = "/zs/rule_edit.do")
    public ReturnData edit(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        User user = (User) req.getSession().getAttribute("user");
        String idStr = req.getParameter("id");

        // 组织参数
        Map<String, Object> map = req.getParameterMap();
        String[] vocabularyStrs = (String[]) map.get("vocabulary");
        String[] characterStrs = (String[]) map.get("character");
        String[] propertyStrs = (String[]) map.get("property");
        String oldCharacter = req.getParameter("old_character");
        String oldProperty = req.getParameter("old_property");

        StringBuilder builder = new StringBuilder();
        String vocabularyStr = "";
        String vocabulary = req.getParameter("sentence");
        for (int index = 0; index < propertyStrs.length; index++) {
            String propertieStr = propertyStrs[index];
            vocabularyStr = vocabularyStrs[index].trim();
            String characterStr = characterStrs[index];

            builder.append(vocabularyStr).append("_").append(characterStr).append("_").append(propertieStr).append(";");
        }
        String ruleType = req.getParameter("rule_type");

        if (ruleType.equals(AnalysisRule.RULE_TYPE_UPDATE_PROPERTY + "")) {
            vocabulary = vocabularyStr + "_" + oldCharacter + "_" + oldProperty;
        }

        ReturnData rtn =  zsRuleService.zsRuleEdit(idStr, vocabulary, ruleType, builder.toString(), user);
        return rtn ;
    }

    /**
     * 删除规则
     *
     * @param req
     * @param resp
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/zs/rule_del.htm")
    public ModelAndView del(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String idStr = req.getParameter("id");

        boolean flag = zsRuleService.del(idStr);
        if (flag) {
            resp.sendRedirect("/zs/rule_list.htm");
        }
        return null;
    }

    /**
     * 进入规则校验列表
     *
     * @param req
     * @param resp
     * @return
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/zs/check_rule_list.htm")
    public ModelAndView checkRuleList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String tabid = req.getParameter("tabid");

        // 维护页面参数到session
        Map<String, Object> pageParam;
        if (tabid != null && !tabid.isEmpty()) {
            pageParam = new HashMap<>();
            pageParam.put("tabid", tabid);
            pageParam.put("start", "1");
            pageParam.put("exactSearchFlag", "0");
            pageParam.put("checkFlag", "0");

            req.getSession().setAttribute("pageParam", pageParam);
        } else {
            pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
        }

        // 获取页面参数，如果request中有值则修改session，否则从session中拿值
        String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
        String keyword = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "keyword");
        String propertyKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "propertyKey");
        String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
        String checkFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "checkFlag");
        Map<String, Object> map =  zsRuleService.checkRuleList(start, keyword, propertyKey, exactSearchFlag, checkFlag);
        return new ModelAndView("zs/check_rule_list", map);
    }

    @ResponseBody
    @RequestMapping(value = "/zs/change_check_flag.do")
    public ReturnData changeCheckFlag(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String type = req.getParameter("type");

        ReturnData rtn =  zsRuleService.changeCheckFlag(id, type);
        return rtn;
    }
}
